library(magrittr)
library(stringr)
library(plyr)
library(tidyverse)
library(readxl)
library(stargazer)
library(multiwayvcov)
library(lmtest)


rm(list=ls())
home = 'C:/Users/Jason/Dropbox/VNA_Responsiveness/Analysis/JOP-dataverse/'


pooled_texts = paste0(home, 'pooled-outcomes.xlsx') %>%
  read_xlsx %>%
  mutate(Citizen=mapvalues(x=Treatment, 
                           from=c('Control',
                                  'Citizen',
                                  'Firm'), 
                           to=c(0,
                                1,
                                0)),
         Citizen=as.integer(Citizen),
         Firm=mapvalues(x=Treatment, 
                        from=c('Control',
                               'Citizen',
                               'Firm'), 
                        to=c(0,
                             0,
                             1)),
         Firm=as.integer(Firm),
         Treatment=factor(x=Treatment, 
                          levels=c('Control',
                                   'Citizen',
                                   'Firm')))
caucus_texts = paste0(home, 'caucus-outcomes.xlsx') %>%
  read_xlsx %>%
  mutate(Citizen=mapvalues(x=Treatment, 
                           from=c('Control',
                                  'Citizen',
                                  'Firm'), 
                           to=c(0,
                                1,
                                0)),
         Citizen=as.integer(Citizen),
         Firm=mapvalues(x=Treatment, 
                        from=c('Control',
                               'Citizen',
                               'Firm'), 
                        to=c(0,
                             0,
                             1)),
         Firm=as.integer(Firm),
         Treatment=factor(x=Treatment, 
                          levels=c('Control',
                                   'Citizen',
                                   'Firm')))
query_texts = paste0(home, 'query-outcomes.xlsx') %>%
  read_xlsx %>%
  mutate(Citizen=mapvalues(x=Treatment, 
                           from=c('Control',
                                  'Citizen',
                                  'Firm'), 
                           to=c(0,
                                1,
                                0)),
         Citizen=as.integer(Citizen),
         Firm=mapvalues(x=Treatment, 
                        from=c('Control',
                               'Citizen',
                               'Firm'), 
                        to=c(0,
                             0,
                             1)),
         Firm=as.integer(Firm),
         Treatment=factor(x=Treatment, 
                          levels=c('Control',
                                   'Citizen',
                                   'Firm')))
floor_texts = paste0(home, 'floor-outcomes.xlsx') %>%
  read_xlsx %>%
  mutate(Citizen=mapvalues(x=Treatment, 
                           from=c('Control',
                                  'Citizen',
                                  'Firm'), 
                           to=c(0,
                                1,
                                0)),
         Citizen=as.integer(Citizen),
         Firm=mapvalues(x=Treatment, 
                        from=c('Control',
                               'Citizen',
                               'Firm'), 
                        to=c(0,
                             0,
                             1)),
         Firm=as.integer(Firm),
         Treatment=factor(x=Treatment, 
                          levels=c('Control',
                                   'Citizen',
                                   'Firm')))


fit_1 = lm(Spoke ~ Citizen + Firm, 
           data=pooled_texts)
fit_2 = lm(Spoke ~ Citizen + Firm + FullTime + CentNom + Competitive, 
           data=pooled_texts)
fit_3 = lm(Spoke ~ Citizen*Prop.Citizen + Firm*Prop.Firm + FullTime + CentNom + Competitive, 
           data=pooled_texts)
fit_4 = lm(Spoke ~ Citizen*Prop.Citizen + Firm*Prop.Firm + FullTime + CentNom + Competitive, 
           data=floor_texts)
fit_5 = lm(Spoke ~ Citizen*Prop.Citizen + Firm*Prop.Firm + FullTime + CentNom + Competitive, 
           data=query_texts)
fit_6 = lm(Spoke ~ Citizen*Prop.Citizen + Firm*Prop.Firm + FullTime + CentNom + Competitive, 
           data=caucus_texts)
fit_7 = lm(Spoke ~ Citizen + Firm + FullTime + CentNom + Competitive, 
           data=subset(pooled_texts,
                       CentNom==0))
fit_8 = lm(Spoke ~ Citizen*Prop.Citizen + Firm*Prop.Firm + FullTime + CentNom + Competitive, 
           data=subset(pooled_texts,
                       CentNom==0))
fit_1_cluster = cluster.vcov(fit_1, ~ Province)
fit_2_cluster = cluster.vcov(fit_2, ~ Province)
fit_3_cluster = cluster.vcov(fit_3, ~ Province)
fit_4_cluster = cluster.vcov(fit_4, ~ Province)
fit_5_cluster = cluster.vcov(fit_5, ~ Province)
fit_6_cluster = cluster.vcov(fit_6, ~ Province)
fit_7_cluster = cluster.vcov(fit_7, ~ Province)
fit_8_cluster = cluster.vcov(fit_8, ~ Province)


stargazer(coeftest(fit_1, fit_1_cluster),
          coeftest(fit_2, fit_2_cluster),
          coeftest(fit_3, fit_3_cluster),
          coeftest(fit_4, fit_4_cluster),
          coeftest(fit_5, fit_5_cluster),
          coeftest(fit_6, fit_6_cluster),
          coeftest(fit_7, fit_7_cluster),
          coeftest(fit_8, fit_8_cluster),
          title='Delegate spoke. Forms the basis of Figures 6, 7, and 10.', 
          covariate.labels=c('Citizen',
                             '% Citizen',
                             'Citizen $times$ % Citizen',
                             'Firm',
                             '% Firm',
                             'Firm $times$ % Firm',
                             'Full-time',
                             'Central Nominee',
                             'Competitive',
                             'Constant'),
          order=c(1:2, 8, 3:4, 9, 5:7, 10),
          align=T, 
          header=F,
          no.space=T, 
          star.cutoffs=NA,
          column.labels=c('Pooled','Floor','Query','Caucus','Pooled (no central nominees)'),
          column.separate=c(3, 1, 1, 1, 2),
          dep.var.labels.include=F, 
          label='appendix-table-05.2',
          notes.append=F, 
          notes='Province-clustered standard errors in parentheses;') %>%
  writeClipboard

